﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.InteropServices;
using System.Data;

using Cisoft.Cisoft3G.Model;
using Cisoft.Cisoft3G.BLL;

public partial class Business_Cisoft3G_OutboxManage_RemindAdd : WebUI.Page.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindTree(0, (TreeNode)null);
            //BindDefineGroup();
            BindTime();
            ltScript.Text = "<script type=\"text/javascript\" src=\"../../../js/My97DatePicker/WdatePicker.js\"></script><script type=\"text/javascript\" src=\"../../../js/MobileJS.js\"></script>";
            txtTime.Attributes.Add("onclick", "WdatePicker({minDate:'%y-%M-{%d}'})");
            txtEndTime.Attributes.Add("onclick", "WdatePicker({minDate:'%y-%M-{%d}'})");
            txtTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            txtEndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            lblStartime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            pnlRateType.Visible = false;
            pnlStandardWeeks.Visible = false;
            pnlEndTime.Visible = false;
            txtTimeNow.Text = DateTime.Now.ToString();


            //手机号码验证
            txtRecive.Attributes.Add("onpropertychange", "return select_onblurAll()");
            txtSMsContent.Attributes.Add("onpropertychange", "MsgContentOnChanging()");
            //添加手机号码验证失败阻止提交事件
            //this.form1.Attributes.Add("onsubmit", "return select_onblurAll()");


            //if (Request.QueryString["id"] != null && Request.QueryString["id"].ToString() != "")
            //{
            //    Cisoft.Cisoft3G.Model.DraftsMO MOshow = new Cisoft.Cisoft3G.Model.DraftsMO();
            //    Cisoft.Cisoft3G.BLL.DraftsBO BOshow = new Cisoft.Cisoft3G.BLL.DraftsBO();
            //    MOshow = BOshow.GetModel(System.Convert.ToInt32(Request.QueryString["id"].ToString()));
            //    if (MOshow != null)
            //    {
            //        txtRecive.Text = MOshow.DT_Receiver;
            //        txtSMsContent.Text = MOshow.DT_SmsContent;
            //    }
            //}

        }
    }

    /// <summary>
    /// 发送短信
    /// </summary>
    protected void btnSend_Click(object sender, EventArgs e)
    {
        int count = new Cisoft.Cisoft3G.BLL.SentBO().GetList(" ST_SentTime >='" + DateTime.Now.ToShortDateString() +
                        " 00:00:00' and ST_SentTime <='" + DateTime.Now.ToShortDateString() + " 23:59:59' ").Tables[0].Rows.Count +
                        new Cisoft.Cisoft3G.BLL.OutboxBO().GetList(" OB_SendTime >='" + DateTime.Now.ToShortDateString() +
                        " 00:00:00' and OB_SendTime <='" + DateTime.Now.ToShortDateString() + " 23:59:59' ").Tables[0].Rows.Count;
        if (count > 5)
        {
            PageError("本日短信配额已用完。", Request.RawUrl);
        }
        else
        {
            //立即发送
            //if (rbtnNow.Checked)
            //{
            //    //SendNowSms();
            //}
            //定时发送
            //if (rbtnTime.Checked)
            //{
            //    //SendTimeSMS();
            //}
        }
    }



    #region 自定义新增方法
    private void SaveRemind()
    {
        string strError = string.Empty;

        string strRemindName = DateTime.Now.ToString("yyyy-MM-dd hhmmss") + "提醒";
        string men = txtRecive.Text.Trim();
        string smsContent = txtSMsContent.Text.Trim();

        Cisoft.Cisoft3G.Model.RemindMO model = new Cisoft.Cisoft3G.Model.RemindMO();
        Cisoft.Cisoft3G.BLL.RemindBO bll = new Cisoft.Cisoft3G.BLL.RemindBO();


        model.RM_RemindID = bll.GetMaxId();
        model.RM_Title = strRemindName;
        model.RM_Recive = men;
        model.RM_SMContent = smsContent;
        model.RM_CreateTime = DateTime.Now;
        model.RM_StartTime = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);
        model.RM_StandardTime = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);
        model.RM_Status = 0;

        if (rbtnEndType0.Checked)
        {
            model.RM_EndType = 0;
            model.RM_EndTime = DateTime.Now;
        }
        else
        {
            model.RM_EndType = 1;
            model.RM_EndTime = Convert.ToDateTime(txtEndTime.Text.Trim() + " 00:00:00");
        }

        model.RM_StandardType = 0;
        model.RM_StandardWeeks = "";

        switch (ddlRateType.SelectedValue)
        {
            case "0":

                model.RM_RateType = 0;

                break;
            case "1":
                model.RM_RateType = 1;

                break;
            case "2":

                model.RM_RateType = 2;
                model.RM_StandardType = 1;
                foreach (ListItem item in cblStandardWeeks.Items)
                {
                    if (item.Selected)
                    {
                        model.RM_StandardWeeks += item.Value;
                    }
                }
                break;
            case "3":
                model.RM_RateType = 3;
                break;
            case "4":
                model.RM_RateType = 4;
                break;
            default:
                model.RM_RateType = 0;
                break;
        }

        try
        {
            bll.Add(model);
        }
        catch (Exception ex)
        {

            strError += ex.Message;
        }

        if (!string.IsNullOrEmpty(strError))
        {
            //PageError("新增提醒失败！失败原因如下： " + strError + "。", Request.RawUrl);
            PageError(ModalPopupExtender1, lblTS, btnBack_ts, "新增提醒失败！失败原因如下： " + strError);
        }
        else
        {
            //PageRight("提醒新增成功！", "RemindManage.aspx", "返回列表", "");
            PageRight(ModalPopupExtender1, lblTS, btnBack_ts, "<h4>提醒新增成功！</h4>", "RemindManage.aspx");
        }
    }
    #endregion

    private void BindTime()
    {
        for (int i = 0; i < 23; i++)
        {
            ListItem item = new ListItem(i.ToString("00"), i.ToString("00"));
            selectHour.Items.Add(item);
        }
        for (int i = 0; i < 59; i++)
        {
            ListItem item = new ListItem(i.ToString("00"), i.ToString("00"));
            selectMinite.Items.Add(item);
        }
    }

    #region 频率类型切换事件
    protected void ddlRateType_SelectedIndexChanged(object sender, EventArgs e)
    {

        switch (ddlRateType.SelectedValue)
        {
            case "0":

                pnlRateType.Visible = false;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "不重复";
                break;
            case "1":

                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "每天 " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "2":

                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = true;

                DateTime time2 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);
                lblRateType.Text = "每周 ";

                foreach (ListItem item in cblStandardWeeks.Items)
                {
                    if (item.Text == Cisoft.IntegrationSystem.Common.DateTimeHelper.GetWeekDay(time2))
                    {
                        item.Selected = true;
                    }
                }

                foreach (ListItem item in cblStandardWeeks.Items)
                {
                    if (item.Selected)
                    {
                        lblRateType.Text += " " + item.Text;
                    }
                }

                lblRateType.Text += selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "3":
                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                DateTime time3 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);

                lblRateType.Text = "每月 第";
                lblRateType.Text += " " + time3.ToString("dd天");
                lblRateType.Text += " " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "4":
                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                DateTime time4 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);

                lblRateType.Text = "每年 的";
                lblRateType.Text += " " + time4.ToString("MM月dd日");
                lblRateType.Text += " " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            default:
                pnlRateType.Visible = false;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "不重复";
                break;
        }
    }
    #endregion

    #region 星期几选择切换事件
    protected void cblStandardWeeks_SelectedIndexChanged(object sender, EventArgs e)
    {
        DateTime time2 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);
        lblRateType.Text = "每周 ";

        foreach (ListItem itemw in cblStandardWeeks.Items)
        {
            if (itemw.Selected)
            {
                lblRateType.Text += " " + itemw.Text;
            }
        }

        lblRateType.Text += selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
    }
    #endregion

    #region 结束时间类型切换事件0
    protected void rbtnEndType0_CheckedChanged(object sender, EventArgs e)
    {
        if (rbtnEndType0.Checked)
        {
            pnlEndTime.Visible = false;
        }
        else
        {
            pnlEndTime.Visible = true;
        }
    }
    #endregion

    #region 结束时间类型切换事件1
    protected void rbtnEndType1_CheckedChanged(object sender, EventArgs e)
    {
        if (rbtnEndType1.Checked)
        {
            pnlEndTime.Visible = true;
        }
        else
        {
            pnlEndTime.Visible = false;
        }
    }
    #endregion

    #region 小时分钟切换事件
    protected void selectHour_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlRateType.SelectedValue)
        {
            case "0":

                pnlRateType.Visible = false;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "不重复";
                break;
            case "1":

                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "每天 " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "2":

                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = true;

                DateTime time2 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);
                lblRateType.Text = "每周 ";

                foreach (ListItem item in cblStandardWeeks.Items)
                {
                    if (item.Text == Cisoft.IntegrationSystem.Common.DateTimeHelper.GetWeekDay(time2))
                    {
                        item.Selected = true;
                    }
                }

                foreach (ListItem item in cblStandardWeeks.Items)
                {
                    if (item.Selected)
                    {
                        lblRateType.Text += " " + item.Text;
                    }
                }

                lblRateType.Text += selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "3":
                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                DateTime time3 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);

                lblRateType.Text = "每月 第";
                lblRateType.Text += " " + time3.ToString("dd天");
                lblRateType.Text += " " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "4":
                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                DateTime time4 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);

                lblRateType.Text = "每年 的";
                lblRateType.Text += " " + time4.ToString("MM月dd日");
                lblRateType.Text += " " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            default:
                pnlRateType.Visible = false;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "不重复";
                break;
        }
    }
    #endregion

    #region 小时分钟切换事件
    protected void selectMinite_SelectedIndexChanged(object sender, EventArgs e)
    {
        switch (ddlRateType.SelectedValue)
        {
            case "0":

                pnlRateType.Visible = false;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "不重复";
                break;
            case "1":

                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "每天 " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "2":

                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = true;

                DateTime time2 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);
                lblRateType.Text = "每周 ";

                foreach (ListItem item in cblStandardWeeks.Items)
                {
                    if (item.Text == Cisoft.IntegrationSystem.Common.DateTimeHelper.GetWeekDay(time2))
                    {
                        item.Selected = true;
                    }
                }

                foreach (ListItem item in cblStandardWeeks.Items)
                {
                    if (item.Selected)
                    {
                        lblRateType.Text += " " + item.Text;
                    }
                }

                lblRateType.Text += selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "3":
                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                DateTime time3 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);

                lblRateType.Text = "每月 第";
                lblRateType.Text += " " + time3.ToString("dd天");
                lblRateType.Text += " " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            case "4":
                pnlRateType.Visible = true;
                pnlStandardWeeks.Visible = false;
                DateTime time4 = Convert.ToDateTime(txtTime.Text.Trim() + " " + selectHour.SelectedItem.Text + ":" + selectMinite.SelectedItem.Text);

                lblRateType.Text = "每年 的";
                lblRateType.Text += " " + time4.ToString("MM月dd日");
                lblRateType.Text += " " + selectHour.SelectedItem.Text + "时" + selectMinite.SelectedItem.Text + "分";
                break;
            default:
                pnlRateType.Visible = false;
                pnlStandardWeeks.Visible = false;
                lblRateType.Text = "不重复";
                break;
        }
    }
    #endregion

    #region 新增事件
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        SaveRemind();
    }
    #endregion

    #region 返回事件
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("RemindManage.aspx");
    }
    #endregion




    #region 呼叫通讯录
    protected void btnCallContacts_Click(object sender, EventArgs e)
    {
        LinkButton1_Click(sender, e);
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        ModalPopupExtender3.Show();
    }
    #endregion

    #region 通讯录 确定添加联系人
    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        ModalPopupExtender3.Hide();
    }
    #endregion

    #region 关闭通讯录
    protected void btnClose_Click(object sender, EventArgs e)
    {
        //if (Session["Contacts"] != null)
        //{
        //    if (!string.IsNullOrEmpty(Session["Contacts"].ToString()))
        //    {
        //        txtRecive.Text = string.IsNullOrEmpty(txtRecive.Text.Trim()) ? "" : txtRecive.Text.TrimEnd(',') + ",";
        //        txtRecive.Text += GetFilterString(Session["Contacts"].ToString());
        //        txtRecive.Text = txtRecive.Text.TrimEnd(',') + ",";
        //    }
        //    Session.Remove("Contacts");
        //}
        //if (Page.IsValid)
        //{
        //    ModalPopupExtender3.Hide();
        //}
        //else
        //{

        //}

        if (!string.IsNullOrEmpty(hih_right.Value))
        {
            txtRecive.Text = string.IsNullOrEmpty(txtRecive.Text.Trim()) ? "" : txtRecive.Text.TrimEnd(',') + ",";
            txtRecive.Text += GetFilterString(hih_right.Value);
            txtRecive.Text = txtRecive.Text.TrimEnd(',') + ",";
        }
        ModalPopupExtender3.Hide();
    }
    #endregion

    #region 处理通讯录字符串
    private string GetFilterString(string input)
    {
        List<string> strList = new List<string>();
        string strFilter = string.Empty;

        string[] strs = input.TrimEnd(',').Split(',');
        foreach (string s in strs)
        {
            try
            {
                Int64 d = Convert.ToInt64(s.Substring(s.Length - 11, 11));
                //检查是否有重复字符串
                if (!txtRecive.Text.Contains(d.ToString()))
                {
                    strList.Add(d.ToString());
                }
            }
            catch (Exception ex)
            {
                continue;
            }
        }
        if (strList.Count > 0)
        {
            strFilter = string.Join(",", strList.ToArray());
        }

        return strFilter;
    }
    #endregion

    #region 绑定TreeView数据
    //绑定TreeView数据
    private void BindTree(int pid, TreeNode node)
    {
        List<ContactGroupMO> modelList = new ContactGroupBO().GetModelList(" CG_ParentID = " + pid + " order by CG_Order desc");
        foreach (ContactGroupMO model in modelList)
        {
            TreeNode tn = new TreeNode();
            if (node == null)
            {
                tn = new TreeNode();
                tn.Text = "<a style='text-decoration:none;' href='GetContactsHandler.ashx?ContactGroupID=" + model.CG_ContactGroupID + "'>" + model.CG_GroupName + "</a>";
                tn.NavigateUrl = "javascript:void(0);";
                tvContactsGroup.Nodes.Add(tn);
                BindTree(model.CG_ContactGroupID, tn);
            }
            else
            {
                tn = new TreeNode();
                tn.Text = "<a style='text-decoration:none;' href='GetContactsHandler.ashx?ContactGroupID=" + model.CG_ContactGroupID + "'>" + model.CG_GroupName + "</a>";
                tn.NavigateUrl = "javascript:void(0);";
                node.ChildNodes.Add(tn);
                BindTree(model.CG_ContactGroupID, tn);
            }
        }
    }
    //绑定自定义组
    private void BindDefineGroup()
    {
        List<DefineGroupMO> list = new DefineGroupBO().GetModelList("");
        foreach (DefineGroupMO model in list)
        {
            lblDefineGroup.Text += string.Format("<tr><td valign='top'><a style='text-decoration:none;' href='{0}'>{1}</a></td></tr>", "GetContactsHandler.ashx?DefineGroupID=" + model.DG_ID, model.DG_Name);
        }
    }
    #endregion
}
